<template>
  <div style="height: 100%;background-color:white ;">
    <el-row>
      <el-col :span="12">
        <el-button size="small" type="success" @click="editWord(null,'new')">新建词语
        </el-button>
      </el-col>
      <el-col :span="12" style="right: auto">
        <div>
          <div style="width: 430px; float: right">
            <el-input v-model="name" placeholder="请输入搜索条件" size="small" @keyup.enter.native="getTableData">
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <el-button slot="append" :icon="appendIcon" v-on:click="switchAppend"></el-button>
            </el-input>
          </div>
          <div v-if="showAdvance" id="advance" style="padding: 10px; float: right">
            <div>
              <div>
                <div style="width: 100px;text-align:right; display: inline-block">
                  <span>分类</span>
                </div>
                <div style="display: inline-block">
                  <el-input v-model="tagName" placeholder="请输入分类" size="small"
                            style="width: 300px;margin-top: 10px"></el-input>
                </div>
              </div>
              <div>
                <div style="width: 100px;text-align:right; display: inline-block">
                  <span>词语来源</span>
                </div>
                <div style="display: inline-block">
                  <el-input v-model="sourceType" placeholder="请输入词语来源" size="small"
                            style="width: 300px;margin-top: 10px"></el-input>
                </div>
              </div>
            </div>
            <div style="margin-top: 60px; right: 10px;">
              <el-row>
                <el-col :offset="7" :span="9">
                  <div style="margin-left: 90px;height: 32px; width: 88px;">
                    <el-button size="small" v-on:click="advanceCancel">取 消</el-button>
                  </div>
                </el-col>
                <el-col :offset="0.8" :span="2">
                  <div div style="margin-left: 5px;height: 32px; width: 88px;">
                    <el-button size="small" type="success" v-on:click="replacement">重 置</el-button>
                  </div>
                </el-col>
                <el-col :offset="2" :span="2">
                  <div div style="margin-left: 5px;height: 32px; width: 88px;">
                    <el-button size="small" type="success" v-on:click="advanceConfirm">确 定</el-button>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <div>
        <el-table :data="tableData" :height="tableHeight" border stripe style="width: 100%;margin-top: 10px"
                  @selection-change="handleSelectionChange">
          <el-table-column
            v-model="multipleSelection"
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="词语名称" min-width="200" prop="name">
            <template slot-scope="scope">
              <span v-html="scope.row.name"></span>
            </template>
          </el-table-column>
          <el-table-column label="英文名称" min-width="100" prop="englishName"></el-table-column>
          <el-table-column label="同义词" min-width="100" prop="synonym">
            <template slot-scope="scope">
              <span v-html="scope.row.synonym"></span>
            </template>
          </el-table-column>
          <el-table-column label="词语分类" min-width="100" prop="tag"></el-table-column>
          <el-table-column label="词语来源" min-width="100" prop="sourceType">
          </el-table-column>
          <el-table-column label="操作" min-width="200">
            <template slot-scope="scope">
               <span><el-link type="primary"
                              @click.prevent="editWord(scope.row,'edit')">编辑</el-link></span>
              <span><el-link type="danger" @click.prevent="del(scope.row.id)">删除</el-link></span>

            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>
    <el-row>
      <div style="margin-top: 10px">
        <el-pagination v-if="multiplePages"
                       :page-size="pageSize"
                       :page-sizes="[5, 10, 15, 20, 50, 100]"
                       :total="totalNum"
                       background
                       layout="total, sizes, prev, pager, next, jumper"
                       style="text-align: right;"
                       @current-change="handleCurrentChange"
                       @size-change="handleSizeChange"></el-pagination>
      </div>
    </el-row>

    <el-dialog :title="titleType" :visible.sync="newDialogVisible" width="500px" @close="closeNewDialog">
      <el-row>
        <el-col :span="6">
          <span class="label">词语名称<em style="color: red">*</em></span>
        </el-col>
        <el-col :span="18">
          <kind-editor :content.sync="newWords.name" editor-type="Input" placeholder="请输入词语"
                       width="100%"/>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px">
        <el-col :span="6">
          <span class="label">英文名称</span>
        </el-col>
        <el-col :span="18">
          <el-input v-model="newWords.englishName" placeholder="请输入英文名称"></el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px">
        <el-col :span="6">
          <span class="label">同义词</span>
        </el-col>
        <el-col :span="18">
          <kind-editor :content.sync="newWords.synonym" editor-type="Input" placeholder="请输入同义词"
                       width="100%"></kind-editor>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px">
        <el-col :span="6">
          <span class="label">分类</span>
        </el-col>
        <el-col :span="18">
          <el-input v-model="newWords.tag" placeholder="请输入分类"></el-input>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeNewDialog">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: "WordsOfficialTable",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      multiplePages: true,
      appendIcon: "el-icon-caret-bottom",
      noData: true,
      totalNum: 0,
      showAdvance: false,
      currentPage: 0, // 第几页
      pageSize: 10, // 每页显示的数据个数
      newDialogVisible: false,
      tagName: '',
      englishName: '',
      synonym: '',
      sourceType: '',
      options: [],
      tableHeight: '',
      name: '',
      titleType: '新建',
      newWords: {},
    }
  },
  methods: {
    advanceCancel() {
      this.showAdvance = false;
    },
    replacement: function () {
      this.name = ''
      this.sourceType = ''
      this.createdBy = ''
      this.tagName = ''

    },
    advanceConfirm() {
      this.getTableData();
      this.showAdvance = false;
    },
    switchAppend: function () {
      if (this.showAdvance) {
        this.showAdvance = false;
        this.appendIcon = "el-icon-caret-bottom";
      } else {
        this.showAdvance = true;
        this.appendIcon = "el-icon-caret-top";
      }
    },
    handleCurrentChange: function (val) {
      this.currentPage = val - 1;
      this.getTableData();
    },
    handleSizeChange(value) {
      this.pageSize = value
      this.getTableData()
    },
    closeNewDialog() {
      this.newDialogVisible = false;
      this.newWords = {}
    },
    editWord(row) {
      if (row) {
        this.titleType = '编辑'
        this.newWords = JSON.parse(JSON.stringify(row))
      } else {
        this.titleType = '新建'
      }
      this.newDialogVisible = true;
    },
    add() {
      if (UTIL.isEmpty(this.newWords.name)) {
        this.$message.warning("请填写词语名称")
        return;
      }
      this.axios.get("/pmph/action/word/addWord", {
        params: {
          words: JSON.stringify(this.newWords)
        }
      }).then(() => {
        this.closeNewDialog()
        this.getTableData()
        this.newWords = {}
      })
    },
    del(id) {
      this.$confirm('您确认要删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.axios.get("/pmph/action/word/delete", {
          params: {
            id: id
          }
        }).then((response) => {
          this.getTableData()
        })
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    getTableData() {
      this.axios.get("/pmph/action/word/selectTableData", {
        params: {
          name: this.name,
          sourceType: this.sourceType,
          tagName: this.tagName,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          radioStatus: 'officialTable'
        }
      }).then((response) => {
        this.totalNum = response.data.totalNum;
        this.tableData = response.data.tableData
        console.log(response)
      })
    }
  },
  created() {
    this.getTableData()
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 95;
  }

}
</script>

<style scoped>

</style>
